<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ConfigController extends Controller
{
    //edit
    public function edit(Request $request)
    {
    	$title = '配置管理';

        //会话控制
        $staff = $request->session()->get('staff');

    	$data = DB::table('bro_config') -> first();
    	return view('admin.conf.config.edit',['title' => $title,'data' => $data,'staff'=>$staff]);
    }
    //update
    public function update(Request $request)
    {
    	$data = $request -> except('_token','oldlogo');
    	// dd($data);
        $oldpath = '.'.$request -> input('oldlogo');
    	 if($request -> hasFile('wz_logo'))//判断是否有文件
       {
            if($request -> file('wz_logo') -> isValid())//判断文件是否有效
            {
                //获取文件的后缀名
                $suffix = $request -> file('wz_logo') -> getClientOriginalExtension();
                // echo $suffix;
                //制作文件名
                $fileName = 'logo'.time().rand().'.'.$suffix;
                //上传文件
                $path = './uploads';//上传文件的目录

                //执行上传
                $request -> file('wz_logo') -> move($path,$fileName);
                //拼凑pic的URL地址
                $data['wz_logo'] = trim($path.'/'.$fileName,'.');
              
                if(file_exists($oldpath))
                {
                    unlink($oldpath);
                }
               
            }
       }

        $res = DB::table('bro_config') -> where('id',$data['id']) -> update($data);

        //判断修改是否成功
        if($res)
        {
            return redirect('/admin/conf/config/edit') -> with('success','更新成功');
        }else{
            return back() -> with('error','更新失败');
        }

    }
}
